﻿using DevComponents.DotNetBar.Controls;
using SharpErp.Data.Common.Entity;
using System;
using System.Collections.Generic;
using System.Windows.Forms;

namespace SharpErp.UI.BasicData
{
    public partial class GoodsMaterialDetailDialog : SharpErp.UI.Common.BaseDialogForm
    {
        public GoodsMaterialDetailDialog()
        {
            InitializeComponent();
            AfterLoad += GoodsMaterialDetailDialog_AfterLoad;
            this.dgLeft.AutoGenerateColumns = this.dgRight.AutoGenerateColumns = false;
        }

        private void GoodsMaterialDetailDialog_AfterLoad(object sender, EventArgs e)
        {
            this.dgLeft.DataSource = this.dgRight.DataSource = Service.GetGoodsMaterialViewByEightClassId(BindType, EightClassObject.Id);
            this.dgLeft.CellClick -= DgLeft_CellClick;
            this.dgRight.CellClick -= DgLeft_CellClick;
            this.dgLeft.CellClick += DgLeft_CellClick;
            this.dgRight.CellClick += DgLeft_CellClick;
        }

        private void DgLeft_CellClick(object sender, System.Windows.Forms.DataGridViewCellEventArgs e)
        {
            var datagrid = sender as DataGridViewX;
            if (datagrid == null || e.RowIndex < 0 || e.ColumnIndex < 0) return;
            for(int i = 0; i < datagrid.RowCount; ++i)
            {
                var cell = datagrid.Rows[e.RowIndex].Cells[0] as DataGridViewCheckBoxCell;
                if (cell != null)
                {
                    cell.Value = (i == e.RowIndex ? cell.TrueValue : cell.FalseValue);
                }
            }
            if(datagrid == dgRight && e.ColumnIndex == 0)
            {
                var item = datagrid.CurrentRow?.DataBoundItem as GoodsMaterialClass;
                if(item != null)
                {
                    var dialog = new UI.Common.CommonMultiSelectDialog();
                    dialog.MultiSelect = true;
                    {
                        dialog.KvKey = KVEntity.KEY_FORMULATION_MATERIAL;
                        dialog.MultiSelect = false;
                        dialog.ReadOnly = true;
                        dialog.SelectedText = item.FormulateName;
                    };
                    if(dialog.ShowDialog() == DialogResult.OK)
                    {
                        item.FormulateName = dialog.SelectedText;
                        item.FormulateId = dialog.SelectedIds.GetEnumerator().Current;
                        Service.UpdateGoodsMaterialClass(item);
                        GoodsMaterialDetailDialog_AfterLoad(null, null);
                    }
                }
            }
        }

        public ItemEightClass EightClassObject { get; set; }

        public const int BIND_TYPE_GOODS        = 1;
        public const int BIND_TYPE_PRODUCTION   = 2;
        public int BindType { get; set; } = BIND_TYPE_GOODS;


        private void buttonUnit_Click(object sender, EventArgs e)
        {
            var dialog = new FormulationEditorDialog();
            dialog.KvKey = Data.Common.Entity.KVEntity.KEY_FORMULATION_MATERIAL;
            dialog.ShowDialog();
        }

        private void buttonAdd_Click(object sender, EventArgs e)
        {
            var dialog = new MaterialInfoSelectDialogV2();
            dialog.SelectType = MaterialInfoSelectDialogV2.SELECTED_TYPE_GOODS_MATERIAL;
            if(dialog.ShowDialog() == System.Windows.Forms.DialogResult.OK)
            {
                var list = new List<GoodsMaterialClass>(dialog.SelectedGoodsMaterial);
                foreach(var item in list)
                {
                    item.ItemEightClassId = EightClassObject.Id;
                    item.BindType = BindType;
                    Service.AddGoodsMaterialClass(item);
                }
                GoodsMaterialDetailDialog_AfterLoad(null, null);
            }
        }

        private void buttonDelete_Click(object sender, EventArgs e)
        {
            var item = dgLeft.CurrentRow?.DataBoundItem as GoodsMaterialClass;
            if(item != null)
            {
                Service.DeleteGoodsMaterialClass(item.Id);
            }
            GoodsMaterialDetailDialog_AfterLoad(null, null);
        }

        private void buttonClose_Click(object sender, EventArgs e)
        {
            this.DialogResult = DialogResult.Cancel;
            this.Close();
        }
    }
}
